/*
 *     LoginServlet.java  Copyright (c) 2021. cc01cc. All rights reserved.
 *
 *     This file is part of e-commerce.
 *
 *     e-commerce is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Affero General Public License as published
 *     by the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     e-commerce is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Affero General Public License for more details.
 *
 *     You should have received a copy of the GNU Affero General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package servlet;

import pojo.Commodity_info;
import pojo.Manager_register;
import pojo.User_register;
import service.ManagerService;
import service.ManagerServiceImpl;
import service.UserService;
import service.UserServiceImpl;
import util.Constant;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//@WebServlet(name = "loginServlet", urlPatterns= "/loginServlet")
public class LoginServlet extends HttpServlet {
    private static final long SerialVersionUid = 1L;
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

        String name = request.getParameter("name");
        String password = request.getParameter("pwd");
        String accountType = accountType = request.getParameter("accountType");

//        System.out.println("name: "+ username + ";password: " + password);

        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        if(accountType.equals("user")){
            UserService userService = new UserServiceImpl();
            //        提取用户名，密码，发送给业务层userService
            User_register user = userService.login(name, password);
            if(user!=null){
                //得到Session，并且设置属性值
                request.getSession().setAttribute(Constant.USER_SESSION, user);
//            Commodity_info commodity_infos [100] ;
                response.sendRedirect("index.jsp");
            }else{
                request.setAttribute("error", "用户名或密码不正确");
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
        }else if(accountType.equals("manager")){
            ManagerService managerService = new ManagerServiceImpl();
            Manager_register manager = managerService.login(name, password);
            if(manager!=null){
                request.getSession().setAttribute(Constant.MANAGER_SESSION, manager);
                response.sendRedirect(request.getContextPath()+"/manager/manager.jsp");
            }else{
                request.setAttribute("error", "用户名或密码不正确");
                request.getRequestDispatcher("login.jsp").forward(request,response);
            }
        }else{
            request.setAttribute("error", "请规范输入");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
}
